package com.ych.modules.api.unAuthorInfc.dict.controller;

import com.ych.modules.base.dbEntity.TSysDictEntity;
import com.ych.modules.base.service.ITSysDictService;
import com.ych.modules.common.service.DictService;
import com.ych.utils.serviceReturn.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2021/1/4
 * Time: 16:33
 * Description:
 */
@RestController("apiDictController")
@RequestMapping("/api/unAuth/dict")
@Api(tags = {"API——码值相关"}, description = "API——码值相关", position = 2)
public class DictController {
    @Autowired
    private DictService      dictService;

    @ApiOperation(value = "获取公共字典type码值", position = 1)
    @GetMapping("/queryByType/{type}")
    public R<List<TSysDictEntity>> queryByType(@ApiParam(name = "type", value = "码值类型", required = true) @PathVariable("type") String type) {
        try {
            if (StringUtils.isBlank(type)) {
                throw new RuntimeException("码值类型不可为空");
            }
            List<TSysDictEntity> dictEntityList = dictService.listDictByType(type);
            if (CollectionUtils.isEmpty(dictEntityList)) {
                throw new RuntimeException("码值类型不存在");
            }
            return new R(0, "获取成功", dictEntityList);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @ApiOperation(value = "获取公共字典types字符串数组码值", position = 2)
    @GetMapping("/queryByTypes/{types}")
    public R<Map<String, List<TSysDictEntity>>> queryByTypes(@ApiParam(name = "types", value = "码值类型", required = true) @PathVariable("types") String[] types) {
        try {
            if (null == types || 0 == types.length) {
                throw new RuntimeException("至少传入一个码值类型");
            }
            Map<String, List<TSysDictEntity>> rtMap = new HashMap<>();
            for (String type : types) {
                List<TSysDictEntity> list = dictService.listDictByType(type);
                if (CollectionUtils.isEmpty(list)) {
                    return new R(2, "码值类型不存在[ " + type + " ]", null);
                }
                rtMap.put(type, list);
            }
            return new R(0, "成功", rtMap);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }
}
